package com.example.rentalcar.controller;


import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.teautil.models.RuntimeOptions;
import com.aliyun.tea.TeaException;
import com.example.rentalcar.entity.VerificationCodeEntry;
import com.example.rentalcar.util.Sample;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/sms")
public class SmsController {
    private static final Map<String, VerificationCodeEntry> verificationCodeStore = new ConcurrentHashMap<>();
    private static final long VERIFICATION_CODE_TTL_MINUTES = 5;
    @GetMapping("/send")
    public String sendSms(@RequestParam String phoneNumber) {

        try {
            // 生成六位随机数作为验证码
            String verificationCode = generateRandomCode(6);
            long expirationTime = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(VERIFICATION_CODE_TTL_MINUTES);
            verificationCodeStore.put(phoneNumber, new VerificationCodeEntry(verificationCode, expirationTime));
            System.out.println(verificationCode);

            // 调用阿里云短信服务发送验证码
            // 这里调用你之前创建的发送短信的方法

            // 假设 Sample.createClient 和 client.sendSmsWithOptions 已经实现
            // 请根据你的实际情况修改
            Client client = Sample.createClient("LTAI5tCPPxidipJatuWWpPWr", "YK1P9sfOJPOwwnx7U34lwTzlQCjtNl");
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setSignName("车游行")
                    .setTemplateCode("SMS_464511008")
                    .setPhoneNumbers(phoneNumber)
                    .setTemplateParam("{\"code\":\"" + verificationCode + "\"}");
            RuntimeOptions runtime = new RuntimeOptions();
            client.sendSmsWithOptions(sendSmsRequest, runtime);

            return "验证码已发送，请注意查收";
        } catch (TeaException error) {
            return "发送验证码失败：" + error.getMessage();
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            return "发送验证码失败：" + error.getMessage();
        }
    }

    @PostMapping("/login")
    public String login(@RequestParam String phoneNumber,@RequestParam String verificationCode) {
        // 在这里实现用户登录逻辑，包括验证手机号和验证码是否匹配
        // 返回登录结果信息
        VerificationCodeEntry codeEntry = verificationCodeStore.get(phoneNumber);
        String VerificationCode=codeEntry.getVerificationCode();
        // 示例：假设验证码为 "1234" 时登录成功
        if (VerificationCode.equals(verificationCode)) {
            return "登录成功";
        } else {
            return "验证码错误，登录失败";
        }
    }

    // 生成指定位数的随机数
    private String generateRandomCode(int digits) {
        Random random = new Random();
        StringBuilder code = new StringBuilder(digits);
        for (int i = 0; i < digits; i++) {
            code.append(random.nextInt(10));
        }
        return code.toString();
    }
}